<?php

/**
 * @Author: king
 * @Date:   2019-05-05 20:14:51
 * @Last Modified by:   king
 * @Last Modified time: 2019-06-04 23:48:14
 */
namespace App\Http\Controllers\Api\v3;

use App\Http\Controllers\Api\BaseController;
use App\Models\Api\LanmeiUserAccount as UserAccountModel;
use App\Models\Api\LanmeiUserAccount;
use App\Models\Api\LanmeiUserAccountRecord;
use App\Models\Api\LanmeiUserBindAlipay;
use App\Exceptions\ApiException;
use Illuminate\Http\Request;


class UserAccountController extends BaseController
{
    /**
     * @return Json
     * @throws ApiException
     */
    public function getAccountInfo()
    {
        $uid = $request->uid;
        // 获取账户信息
        $accountInfo = (new UserAccountModel())->getAccountInfoAndCreate($uid);

        $toast = $accountInfo['status'] == 2 ? '账户已冻结' : '';
        // todo 获取支付宝绑定信息
        $aliNumberInfo = (new LanmeiUserBindAlipay())->getAliNumberInfo($uid);

        $result = [
            'balance' => (float)$accountInfo['balance'],
            'alipay_account' => $aliNumberInfo['alipay_account'],
            'status' => $accountInfo['status'],
            //'num' => 1,// 剩余提现次数
            'toast' => $toast,
            'msg' => '',
            'withdraw_tips' => '提现到账时间为每周一，节假日顺延。',
            'withdraw_protocol' => 'www.baidu.com'
        ];

        return show(config('code.success'), '请求成功', $result);
    }

    /**
     * 账户明细
     * @throws ApiException
     */
    public function getAccountRecordList()
    {
        $uid = $request->uid;
        // 获取账户ID
        $accountId = (new LanmeiUserAccount())->where('uid', $uid)->value('id');
       // var_dump($accountId);die;
        // 获取账户流水
        $data = LanmeiUserAccountRecord::getList($accountId);
        // 流水数据重组
        $data = LanmeiUserAccountRecord::dataReset($data);
        if(!$data){
         return show(config('code.success'), '没有找到相关数据');
        }

        return show(config('code.success'), '请求成功', $data);
    }

    /**
     * 明细详情
     * @return Json
     * @throws ApiException
     */
    public function getAccountRecodeInfo(Request $request)
    {
        $rid = $request->input('rid', 0);

        if (!$rid)
        {
            return show(config('code.error'), '流水异常');
        }

        $data = LanmeiUserAccountRecord::getInfo($rid);

        return show(config('code.success'), '请求成功', $data);
    }
}
